package com.whu.MenuHub.controller;

import com.whu.MenuHub.domain.UserFlavor;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.UserFlavorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/userflavor")
@Api(tags = "用户口味偏好信息管理API")
public class UserFlavorController {
    @Autowired
    UserFlavorService userFlavorService;

    @GetMapping("/id/{id}")
    @ApiOperation("根据id获取用户口味偏好信息")
    @PreAuthorize("hasAnyAuthority('all',#id.toString())")
    public UserFlavor getUserflavorByID(@ApiParam("用户ID")@PathVariable Integer id){
        return userFlavorService.getfalvorByUserID(id);
    }

    @PostMapping("")
    @ApiOperation("新增一条用户口味偏好信息")
    @PreAuthorize("hasAnyAuthority(#userFlavor.userID.toString())")
    public UserFlavor InsertUserflavor(@ApiParam("用户口味信息")@RequestBody UserFlavor userFlavor) throws MenuHubException {
        return userFlavorService.insertUserFalvor(userFlavor);
    }

    @PutMapping("")
    @ApiOperation("修改一条用户口味偏好记录")
    @PreAuthorize("hasAnyAuthority(#userFlavor.userID.toString())")
    public void updateUserFlavor(@ApiParam("用户口味信息")@RequestBody UserFlavor userFlavor) throws MenuHubException {
        userFlavorService.updateuserFlavor(userFlavor);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除一条用户口味偏好记录")
    @PreAuthorize("hasAnyAuthority(#id.toString())")
    public void deleteUserFlavor(@ApiParam("用户ID")@PathVariable Integer id) throws MenuHubException {
        userFlavorService.deleteUserFlavorById(id);
    }
}
